import requests
import json
import chardet
import re


def get_cid(bvid):
    url = 'https://api.bilibili.com/x/player/pagelist?bvid=' + bvid + '&jsonp=jsonp'
    res = requests.get(url).text  # 获取数据
    json_dict = json.loads(res)  # 将数据转为json
    return json_dict["data"][0]["cid"]  # 获取cid并返回


# BV1jC4y147rv

def get_data(cid):
    final_url = "https://api.bilibili.com/x/v1/dm/list.so?oid=" + str(cid)
    final_res = requests.get(final_url)
    final_res.encoding = chardet.detect(final_res.content)['encoding']
    final_res = final_res.text
    pattern = re.compile('<d.*?>(.*?)</d>')
    data = pattern.findall(final_res)
    return data


def save_to_file(data):
    with open("data.txt", mode="w", encoding="utf-8") as f:
        for i in data:
            f.write(i)
            f.write("\n")


def begin(bvid):
    try:
        cid = get_cid(bvid)
        print(cid)
        data = get_data(cid)
        save_to_file(data)
        print("爬取成功!!!!")
        return True
    except:
        return False


begin("BV1h7411p7Hd")
